package xt.outfood.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.search.similarities.Lambda;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import xt.outfood.common.Result;
import xt.outfood.dto.SetmealDto;
import xt.outfood.entity.Category;
import xt.outfood.entity.Dish;
import xt.outfood.entity.Setmeal;
import xt.outfood.service.CategoryService;
import xt.outfood.service.SetmealDishService;
import xt.outfood.service.SetmealService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 *
 * @author 小小爱欧
 * @date 2022/8/10 14:32
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 套餐管理分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page> getPage(int page, int pageSize, String name) {
        /**
         * 先构造分页器，再构造查询条件，最后执行查询
         */
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //根据条件进行模糊查询
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime).orderByAsc(Setmeal::getCreateTime);
        setmealService.page(setmealPage, queryWrapper);
        BeanUtils.copyProperties(setmealPage, dtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //拷贝
            BeanUtils.copyProperties(item, setmealDto);
            //获取分类id
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //获取分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;

        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return Result.success(dtoPage);
    }

    /**
     * 添加套餐，添加套餐的时候也把该类下的所有套餐删除
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setMealCache",allEntries = true)
    public Result<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("接受到的参数为:{}", setmealDto);
        setmealService.saveWithDash(setmealDto);
        return Result.success("添加套餐成功成功");
    }

    /**
     * 删除某个套餐的时候，删除这个套餐分类下的所有缓存数据
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setMealCache",allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return Result.success("套餐数据删除成功");
    }

    /**
     * 添加查询缓存。利用@Cachable注解添加缓存
     * 动态拼接缓存的key，key需为实体中存在的字段
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setMealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public Result<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();
        queryWrapper.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(status!=null,Setmeal::getStatus,status);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return Result.success(setmealList);
    }

}
